//
//  GlobalFormVC.swift
//  ZYA
//
//  Created by 邱仙凯 on 2021/10/31.
//  Copyright © 2021 aowang. All rights reserved.
//

import UIKit
 
class GlobalFormVC: UIViewController {
    lazy var tableVw:UITableView = {
        let table = UITableView(frame:self.view.bounds)
        table.delegate = self
        table.dataSource = self
        table.backgroundColor = Theme.commonBg
        table.showsVerticalScrollIndicator = false
        return table
    }()
    lazy var header:UIView = {
        let v = UIView(frame:CGRect(x:0,y:0,width: KSGlobal.screenW,height:KSGlobal.screenH))
        v.backgroundColor = Theme.commonBg
        return v
    }()
    lazy var bg :FormView = {
        let v = FormView(frame:CGRect(x:BgLeading,y:10,width: KSGlobal.screenW - BgLeading * 2,height:KSGlobal.screenH))
        v.backgroundColor = .clear
        v.layer.cornerRadius = ItemCornerRadius
        return v
    }()
    
    let btnTopPadding:CGFloat = 18
    // 有细表时可以用这保存控件，添加到footer
    lazy var footSubmit: UIView = {
        let v = UIView(frame: CGRect(x: BgLeading, y: 0, width: KSGlobal.screenW - BgLeading * 2, height: ItemBtnHeight + btnTopPadding))
        v.backgroundColor = .clear
         v.addSubview(submitButton)
        submitButton.ks_y = v.ks_y + btnTopPadding
        return v
    }()
    lazy var footSubmitAndDraft: UIView = {
        let v = UIView(frame: CGRect(x: BgLeading, y: 0, width: KSGlobal.screenW - BgLeading * 2, height: ItemBtnHeight * 2 + btnTopPadding))
        v.backgroundColor = .clear
         v.addSubview(submitButton)
        submitButton.ks_y = v.ks_y + btnTopPadding
        
        v.addSubview(submitDraftButton)
        submitDraftButton.ks_y = submitButton.ks_maxY
        
        
        return v
    }()
    
    
    
    lazy var footSubmitAndAdd: UIView = {
        let v = UIView(frame: CGRect(x: BgLeading, y: 0, width: KSGlobal.screenW - BgLeading * 2, height: ItemBtnHeight * 2 + btnTopPadding))
        v.backgroundColor = .clear
         v.addSubview(submitButton)
        submitButton.ks_y = v.ks_y + btnTopPadding
        v.addSubview(submitAndAddBtn)
        submitAndAddBtn.ks_y = submitButton.ks_maxY
        return v
    }()
    
    lazy var footSubmitAndDelete: UIView = {
        let v = UIView(frame: CGRect(x: BgLeading, y: 0, width: KSGlobal.screenW - BgLeading * 2, height: ItemBtnHeight * 2 + btnTopPadding))
        v.backgroundColor = .clear
         v.addSubview(submitButton)
        submitButton.ks_y = v.ks_y + btnTopPadding
        v.addSubview(deleteButton)
        deleteButton.ks_y = submitButton.ks_maxY
        return v
    }()
  
    lazy var submitButton: KSUIButton = {
        let button = KSUIButton(titlex: "保存")
         return button
    }()
    lazy var deleteButton: KSUIButton = {
        let button = KSUIButton(titlex: "删除")
         return button
    }()
    lazy var submitDraftButton: KSUIButton = {
        let button = KSUIButton(titlex: "保存草稿")
         return button
    }()
    lazy var submitAndAddBtn: KSUIButton = {
        let button = KSUIButton(titlex: "保存并新增")
        return button
    }()
    lazy var searchButton: KSUIButton = {
        let button = KSUIButton(titlex: "查询")
        return button
    }()
    
    lazy var supplementBtn: KSUIButton = {
        let button = KSUIButton(titlex: "补录照片")
        return button
    }()
    lazy var confirmUnauditBtn: KSUIButton = {
        let button = KSUIButton(titlex: "确认消审")
        return button
    }()
    lazy var dateTimePick:DateTimePicker = {
        let v = DateTimePicker()
        return v
    }()
     var comPick:KSComPicker?
    var imageUtil:KSImageSelectUtil?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(tableVw)
        tableVw.tableHeaderView = header
        tableVw.separatorStyle = .none
        tableVw.tableFooterView = UIView()
        tableVw.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: TabbarHeight + 150, right: 0)
        tableVw.register(GlobalAddTableCell.self, forCellReuseIdentifier: GlobalAddTableCell.identifier)
        header.addSubview(bg)
        if #available(iOS 15.0, *) {
            tableVw.sectionHeaderTopPadding = 0
        }
    }
    
    func setFooterAndReload(_ view:UIView) {
        self.tableVw.tableFooterView = view
        self.tableVw.reloadData()
    }
    // 表单判空
    func judgeNoNone() -> Bool {
        let vali = Validation()
        for sub in bg.subviews {
            if sub.isKind(of: KSFormBaseItem.self) {
                let subV = sub as! KSFormBaseItem
                if subV.isMustInput() {
                    vali.required(value: subV.contValue(), msg: "\(subV.tpTitleLab.text.unwrap)不为空")
                }
            }
        }
        guard vali.validate() else {
            return false
        }
        return true
    }
    
}
extension GlobalFormVC:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath) as? UITableViewCell else {
            fatalError()
        }
        return cell
    }
    
}
class KSUIButton:FormItem {
    lazy var  button: UIButton = {
        let button = UIButton()
        button.layer.cornerRadius = 8
        button.backgroundColor = UIColor(kshex: "#4972F9")
        return button
    }()
    
    init(titlex:String) {
        super.init(frame: CGRect(origin: .zero, size: CGSize(width: KSGlobal.screenW - BgLeading * 2, height: ItemBtnHeight)))
        backgroundColor = .clear
        self.setLineHidden(true)
        button.setTitle(titlex, for: UIControl.State())
        addSubview(button)
        button.snp.makeConstraints { make in
            make.centerX.centerY.equalTo(self)
            make.height.equalToSuperview().multipliedBy(0.8)
            make.width.equalTo(KSGlobal.screenW - 42)
        }
    }
    @discardableResult
    func setRoundType(roundType:KSRoundType) -> KSUIButton {
        if  roundType == .all {
            self.setMutiBorderRoundingCorners(corner: [.topLeft,.topRight,.bottomLeft,.bottomRight], number: ItemCornerRadius)
            
        } else if  roundType == .top {
            self.setMutiBorderRoundingCorners(corner: [.topLeft,.topRight], number: ItemCornerRadius)
            
        } else if  roundType == .bottom {
            self.setMutiBorderRoundingCorners(corner: [.bottomLeft,.bottomRight], number: ItemCornerRadius)
            
        }
        return self
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// 统一处理高度  *配置菜单高度流程*
func getHeightFromItem(item:FormItem? = nil,appearance: KSFormBaseAppear? = KSFormBaseAppear(),type:KType = .tempTextView) -> CGFloat {
    var heightx:CGFloat = 0
    if type == .singleSelect {
        let count = appearance!.selectConArr!.count
        heightx = CGFloat(30 * count) + 6
    } else {
        heightx = appearance!.contentHight
    }
  
    // 最后收尾，最低不能比基础高度低
    if appearance! is KSFormVAppear {
    
         if heightx < ItemVHeight {
            heightx = ItemVHeight
        }
        
    } else {
        var isSeperteItem = false
        if item.isSome {
            if item!.isKind(of: KSSeperateItem.self) {
                isSeperteItem = true
            }
        }
        
        if type == .seperate || isSeperteItem {
            
        } else {
           // 除了分界线可以小于单个 item的高度，其他的最小为单个item高
                if heightx < ItemHHeight {
                    heightx = ItemHHeight
                }
        }
        
     }
      return heightx
}
